programming4us
           
 
 
SQL Server

Using XML in SQL Server 2008: Relational Data As XML - The FOR XML Modes (part 3) - AUTO Mode

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
1/10/2011 11:43:52 AM

AUTO Mode

When RAW mode is not enough, FOR XML AUTO provides a few more ways to shape your XML output. Its usefulness derives from its capability to produce nested XML elements from rows derived by joining multiple tables, in contrast to the flat structure of RAW mode.

The ROOT keyword introduced earlier also applies with AUTO mode, and it is good practice to continue to use it in your queries. Like RAW mode, AUTO mode produces attribute-centric XML by default, but you can change this by using the ELEMENTS keyword. XSINIL and XMLSCHEMA are also applicable here, having the same effect as with RAW mode. Listing 6 illustrates these points.

Listing 6. A SELECT Statement That Uses FOR XML AUTO, ELEMENTS XSINIL, ROOT
SELECT
Color,
Offer.SpecialOfferId Id,
Product.ProductId Id,
Name,
Description [Desc],
Size
FROM Sales.SpecialOffer Offer
JOIN Sales.SpecialOfferProduct OP ON
OP.SpecialOfferId = Offer.SpecialOfferId
JOIN Production.Product Product ON
Product.ProductId = OP.ProductId
WHERE Name LIKE 'Mountain Bike%'
FOR XML AUTO, ELEMENTS XSINIL, ROOT('MountainBikeSpecials')
go
<MountainBikeSpecials xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Product>
<Color>White</Color>
<Id>710</Id>
<Name>Mountain Bike Socks, L</Name>
<Size>L</Size>
<Offer>
<Id>1</Id>
<Desc>No Discount</Desc>
</Offer>
</Product>
<Product>
<Color>White</Color>
<Id>709</Id>
<Name>Mountain Bike Socks, M</Name>
<Size>M</Size>
<Offer>
<Id>1</Id>
<Desc>No Discount</Desc>
</Offer>
<Offer>
<Id>2</Id>
<Desc>Volume Discount 11 to 14</Desc>
</Offer>
<Offer>
<Id>3</Id>
<Desc>Volume Discount 15 to 24</Desc>
</Offer>
<Offer>
<Id>4</Id>
<Desc>Volume Discount 25 to 40</Desc>
</Offer>
</Product>
</MountainBikeSpecials>


With AUTO mode, the keywords BINARY BASE64 have the same effect as with RAW mode, with one major difference: RAW mode generates an error if binary data is selected and BINARY BASE64 is not specified; therefore, it is required. With AUTO mode, binary data may be selected without specifying BINARY BASE64, although SQL Server requires that the primary key of the table containing the binary data be selected. This is so that SQL Server can generate a path to the binary field, using the primary key to address the row (in place of the encoded data), of the following form:

'dbobject/SchemaName.TableName[@PrimaryKeyName="PrimaryKeyValue"]/@ColumnName'

This special XPath-like output is unique to AUTO mode and is useful for applications that incorporate SQLXML’s URL-based querying to return the desired binary data. Listing 7 illustrates this XML production.
Listing 7. Addressing Binary Data That Uses FOR XML AUTO
SELECT Top 1
Photo.ProductPhotoId, ThumbNailPhoto, Color, Offer.SpecialOfferId Id,
Product.ProductId Id, Name, Description [Desc], Size
FROM Sales.SpecialOffer Offer
JOIN Sales.SpecialOfferProduct OP ON
OP.SpecialOfferId = Offer.SpecialOfferId
JOIN Production.Product Product ON
Product.ProductId = OP.ProductId
JOIN Production.ProductProductPhoto PhotoJunction ON
Product.ProductId = PhotoJunction.ProductId
JOIN Production.ProductPhoto Photo ON
Photo.ProductPhotoId = PhotoJunction.ProductPhotoId
WHERE Name LIKE 'Mountain Bike%'
FOR XML AUTO, ELEMENTS XSINIL, ROOT('MountainBikeSpecials')
go
<MountainBikeSpecials xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Photo>
<ProductPhotoId>1</ProductPhotoId>
<ThumbNailPhoto>
dbobject/Production.ProductPhoto[@ProductPhotoID='1']/@ThumbNailPhoto
</ThumbNailPhoto>
<Product>
<Color>White</Color>
<Id>710</Id>
<Name>Mountain Bike Socks, L</Name>
<Size>L</Size>
<Offer>
<Id>1</Id>
<Desc>No Discount</Desc>
</Offer>
</Product>
</Photo>
</MountainBikeSpecials>

Notice how you can generate an additional level of nesting (with the Photo element) in the XML hierarchy simply by selecting a value from an additional table.

SQL Server has a set of rules it uses for nesting elements in AUTO mode. As rows are streamed to output, the XML engine studiously compares the values in adjacent columns to check for differences from the first row on down to the last. When one or more primary keys have been selected in the query, only the primary key values are used in the column comparison. When no primary keys have been selected, all column values are used in the comparison, except for columns of type ntext, text, image, or xml, whose values are always assumed to be different.

The following example includes primary keys in the SELECT statement:

SELECT Offer.SpecialOfferId, Product.ProductId, Name
FROM Sales.SpecialOffer Offer
JOIN Sales.SpecialOfferProduct OP ON
OP.SpecialOfferId = Offer.SpecialOfferId
JOIN Production.Product Product ON
Product.ProductId = OP.ProductId
WHERE Name LIKE 'Mountain Bike%'
go
SpecialOfferId ProductId Name
----------------------------------------------------
1 710 Mountain Bike Socks, L
1 709 Mountain Bike Socks, M
2 709 Mountain Bike Socks, M
3 709 Mountain Bike Socks, M
4 709 Mountain Bike Socks, M
(5 row(s) affected)

As the XML engine works down this result set, it sees that SpecialOfferId has the same value in the first and second rows, but ProductId differs in the same rows. It therefore creates one Offer element and nests the two different Product values in Product subelements.

Column selection order is also a determining factor in AUTO mode XML composition. Notice that even though in Rows 2–5, the ProductId remains 709, the XML engine still nests Product under Offer because Offer.SpecialOfferId is specified first in the list of selected columns. When FOR XML AUTO is added to the preceding query, it results in the following:

<MountainBikeSpecials>
<Offer SpecialOfferId="1">
<Product ProductId="710" Name="Mountain Bike Socks, L" />
<Product ProductId="709" Name="Mountain Bike Socks, M" />
</Offer>
<Offer SpecialOfferId="2">
<Product ProductId="709" Name="Mountain Bike Socks, M" />
</Offer>
<Offer SpecialOfferId="3">
<Product ProductId="709" Name="Mountain Bike Socks, M" />
</Offer>
<Offer SpecialOfferId="4">
<Product ProductId="709" Name="Mountain Bike Socks, M" />
</Offer>
</MountainBikeSpecials>

To tell the XML engine that you prefer to nest Offer under Product, you simply change the column order in the SELECT statement:

SELECT Product.ProductId, Offer.SpecialOfferId, Name
FROM Sales.SpecialOffer Offer
JOIN Sales.SpecialOfferProduct OP ON
OP.SpecialOfferId = Offer.SpecialOfferId
JOIN Production.Product Product ON
Product.ProductId = OP.ProductId
WHERE Name LIKE 'Mountain Bike%'
FOR XML AUTO, ROOT('MountainBikeSpecials')
go
<MountainBikeSpecials>
<Product ProductId="710" Name="Mountain Bike Socks, L">
<Offer SpecialOfferId="1" />
</Product>
<Product ProductId="709" Name="Mountain Bike Socks, M">
<Offer SpecialOfferId="1" />
<Offer SpecialOfferId="2" />
<Offer SpecialOfferId="3" />
<Offer SpecialOfferId="4" />
</Product>
</MountainBikeSpecials>
Other -----------------
- Programming with SQL Azure : WCF Data Services (part 1)
- Programming with SQL Azure : Connecting to SQL Azure (part 4) - Sqlcmd
- Programming with SQL Azure : Connecting to SQL Azure (part 3) - ODBC
- Programming with SQL Azure : Connecting to SQL Azure (part 2)
- Programming with SQL Azure : Connecting to SQL Azure (part 1) - ADO.NET
- Programming with SQL Azure : Application Deployment Factors
- SQL Server 2008: SQL Server Web Services - Building Web Services (part 3)
- SQL Server 2008: SQL Server Web Services - Building Web Services (part 2)
- SQL Server 2008: SQL Server Web Services - Building Web Services (part 1)
- SQL Server 2008: SQL Server Web Services
- SQL Server 2008: SQL Server Service Broker - Related System Catalogs
- SQL Azure Backup Strategies (part 2)
- SQL Azure Backup Strategies (part 1) - Copying a Database
- SQL Server 2008: Troubleshooting SSB Applications with ssbdiagnose.exe
- SQL Server 2008: Service Broker Routing and Security
- Migrating Databases and Data to SQL Azure (part 9)
- Migrating Databases and Data to SQL Azure (part 8)
- Understanding Service Broker Constructs (part 5)
- Understanding Service Broker Constructs (part 4) - Creating the Conversation Initiator
- Migrating Databases and Data to SQL Azure (part 7)
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us